home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb-4.5 / sun4.md / gdb / RCS / values.c,v < prev    next >
Encoding:
Text File  |  1992-06-24  |  41.0 KB  |  1,503 lines

  1. head     1.1;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.1
  10. date     92.06.09.14.26.51;  author secor;  state Exp;
  11. branches ;
  12. next     ;
  13.  
  14.  
  15. desc
  16. @@
  17.  
  18.  
  19.  
  20. 1.1
  21. log
  22. @Initial revision
  23. @
  24. text
  25. @/* Low level packing and unpacking of values for GDB, the GNU Debugger.
  26.    Copyright 1986, 1987, 1989, 1991 Free Software Foundation, Inc.
  27.  
  28. This file is part of GDB.
  29.  
  30. This program is free software; you can redistribute it and/or modify
  31. it under the terms of the GNU General Public License as published by
  32. the Free Software Foundation; either version 2 of the License, or
  33. (at your option) any later version.
  34.  
  35. This program is distributed in the hope that it will be useful,
  36. but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  38. GNU General Public License for more details.
  39.  
  40. You should have received a copy of the GNU General Public License
  41. along with this program; if not, write to the Free Software
  42. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  43.  
  44. #include "defs.h"
  45. #include <string.h>
  46. #include "symtab.h"
  47. #include "gdbtypes.h"
  48. #include "value.h"
  49. #include "gdbcore.h"
  50. #include "frame.h"
  51. #include "command.h"
  52. #include "gdbcmd.h"
  53. #include "target.h"
  54.  
  55. /* Local function prototypes. */
  56.  
  57. static value
  58. value_headof PARAMS ((value, struct type *, struct type *));
  59.  
  60. static void
  61. show_values PARAMS ((char *, int));
  62.  
  63. static void
  64. show_convenience PARAMS ((char *, int));
  65.  
  66. /* The value-history records all the values printed
  67.    by print commands during this session.  Each chunk
  68.    records 60 consecutive values.  The first chunk on
  69.    the chain records the most recent values.
  70.    The total number of values is in value_history_count.  */
  71.  
  72. #define VALUE_HISTORY_CHUNK 60
  73.  
  74. struct value_history_chunk
  75. {
  76.   struct value_history_chunk *next;
  77.   value values[VALUE_HISTORY_CHUNK];
  78. };
  79.  
  80. /* Chain of chunks now in use.  */
  81.  
  82. static struct value_history_chunk *value_history_chain;
  83.  
  84. static int value_history_count;    /* Abs number of last entry stored */
  85.  
  86. /* List of all value objects currently allocated
  87.    (except for those released by calls to release_value)
  88.    This is so they can be freed after each command.  */
  89.  
  90. static value all_values;
  91.  
  92. /* Allocate a  value  that has the correct length for type TYPE.  */
  93.  
  94. value
  95. allocate_value (type)
  96.      struct type *type;
  97. {
  98.   register value val;
  99.  
  100.   check_stub_type (type);
  101.  
  102.   val = (value) xmalloc (sizeof (struct value) + TYPE_LENGTH (type));
  103.   VALUE_NEXT (val) = all_values;
  104.   all_values = val;
  105.   VALUE_TYPE (val) = type;
  106.   VALUE_LVAL (val) = not_lval;
  107.   VALUE_ADDRESS (val) = 0;
  108.   VALUE_FRAME (val) = 0;
  109.   VALUE_OFFSET (val) = 0;
  110.   VALUE_BITPOS (val) = 0;
  111.   VALUE_BITSIZE (val) = 0;
  112.   VALUE_REPEATED (val) = 0;
  113.   VALUE_REPETITIONS (val) = 0;
  114.   VALUE_REGNO (val) = -1;
  115.   VALUE_LAZY (val) = 0;
  116.   VALUE_OPTIMIZED_OUT (val) = 0;
  117.   return val;
  118. }
  119.  
  120. /* Allocate a  value  that has the correct length
  121.    for COUNT repetitions type TYPE.  */
  122.  
  123. value
  124. allocate_repeat_value (type, count)
  125.      struct type *type;
  126.      int count;
  127. {
  128.   register value val;
  129.  
  130.   val = (value) xmalloc (sizeof (struct value) + TYPE_LENGTH (type) * count);
  131.   VALUE_NEXT (val) = all_values;
  132.   all_values = val;
  133.   VALUE_TYPE (val) = type;
  134.   VALUE_LVAL (val) = not_lval;
  135.   VALUE_ADDRESS (val) = 0;
  136.   VALUE_FRAME (val) = 0;
  137.   VALUE_OFFSET (val) = 0;
  138.   VALUE_BITPOS (val) = 0;
  139.   VALUE_BITSIZE (val) = 0;
  140.   VALUE_REPEATED (val) = 1;
  141.   VALUE_REPETITIONS (val) = count;
  142.   VALUE_REGNO (val) = -1;
  143.   VALUE_LAZY (val) = 0;
  144.   VALUE_OPTIMIZED_OUT (val) = 0;
  145.   return val;
  146. }
  147.  
  148. /* Return a mark in the value chain.  All values allocated after the
  149.    mark is obtained (except for those released) are subject to being freed
  150.    if a subsequent value_free_to_mark is passed the mark.  */
  151. value
  152. value_mark ()
  153. {
  154.   return all_values;
  155. }
  156.  
  157. /* Free all values allocated since MARK was obtained by value_mark
  158.    (except for those released).  */
  159. void
  160. value_free_to_mark (mark)
  161.      value mark;
  162. {
  163.   value val, next;
  164.  
  165.   for (val = all_values; val && val != mark; val = next)
  166.     {
  167.       next = VALUE_NEXT (val);
  168.       value_free (val);
  169.     }
  170.   all_values = val;
  171. }
  172.  
  173. /* Free all the values that have been allocated (except for those released).
  174.    Called after each command, successful or not.  */
  175.  
  176. void
  177. free_all_values ()
  178. {
  179.   register value val, next;
  180.  
  181.   for (val = all_values; val; val = next)
  182.     {
  183.       next = VALUE_NEXT (val);
  184.       value_free (val);
  185.     }
  186.  
  187.   all_values = 0;
  188. }
  189.  
  190. /* Remove VAL from the chain all_values
  191.    so it will not be freed automatically.  */
  192.  
  193. void
  194. release_value (val)
  195.      register value val;
  196. {
  197.   register value v;
  198.  
  199.   if (all_values == val)
  200.     {
  201.       all_values = val->next;
  202.       return;
  203.     }
  204.  
  205.   for (v = all_values; v; v = v->next)
  206.     {
  207.       if (v->next == val)
  208.     {
  209.       v->next = val->next;
  210.       break;
  211.     }
  212.     }
  213. }
  214.  
  215. /* Return a copy of the value ARG.
  216.    It contains the same contents, for same memory address,
  217.    but it's a different block of storage.  */
  218.  
  219. value
  220. value_copy (arg)
  221.      value arg;
  222. {
  223.   register value val;
  224.   register struct type *type = VALUE_TYPE (arg);
  225.   if (VALUE_REPEATED (arg))
  226.     val = allocate_repeat_value (type, VALUE_REPETITIONS (arg));
  227.   else
  228.     val = allocate_value (type);
  229.   VALUE_LVAL (val) = VALUE_LVAL (arg);
  230.   VALUE_ADDRESS (val) = VALUE_ADDRESS (arg);
  231.   VALUE_OFFSET (val) = VALUE_OFFSET (arg);
  232.   VALUE_BITPOS (val) = VALUE_BITPOS (arg);
  233.   VALUE_BITSIZE (val) = VALUE_BITSIZE (arg);
  234.   VALUE_REGNO (val) = VALUE_REGNO (arg);
  235.   VALUE_LAZY (val) = VALUE_LAZY (arg);
  236.   if (!VALUE_LAZY (val))
  237.     {
  238.       bcopy (VALUE_CONTENTS_RAW (arg), VALUE_CONTENTS_RAW (val),
  239.          TYPE_LENGTH (VALUE_TYPE (arg))
  240.          * (VALUE_REPEATED (arg) ? VALUE_REPETITIONS (arg) : 1));
  241.     }
  242.   return val;
  243. }
  244.  
  245. /* Access to the value history.  */
  246.  
  247. /* Record a new value in the value history.
  248.    Returns the absolute history index of the entry.
  249.    Result of -1 indicates the value was not saved; otherwise it is the
  250.    value history index of this new item.  */
  251.  
  252. int
  253. record_latest_value (val)
  254.      value val;
  255. {
  256.   int i;
  257.  
  258.   /* Check error now if about to store an invalid float.  We return -1
  259.      to the caller, but allow them to continue, e.g. to print it as "Nan". */
  260.   if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FLT) {
  261.     (void) unpack_double (VALUE_TYPE (val), VALUE_CONTENTS (val), &i);
  262.     if (i) return -1;        /* Indicate value not saved in history */
  263.   }
  264.  
  265.   /* Here we treat value_history_count as origin-zero
  266.      and applying to the value being stored now.  */
  267.  
  268.   i = value_history_count % VALUE_HISTORY_CHUNK;
  269.   if (i == 0)
  270.     {
  271.       register struct value_history_chunk *new
  272.     = (struct value_history_chunk *)
  273.       xmalloc (sizeof (struct value_history_chunk));
  274.       bzero (new->values, sizeof new->values);
  275.       new->next = value_history_chain;
  276.       value_history_chain = new;
  277.     }
  278.  
  279.   value_history_chain->values[i] = val;
  280.   release_value (val);
  281.  
  282.   /* Now we regard value_history_count as origin-one
  283.      and applying to the value just stored.  */
  284.  
  285.   return ++value_history_count;
  286. }
  287.  
  288. /* Return a copy of the value in the history with sequence number NUM.  */
  289.  
  290. value
  291. access_value_history (num)
  292.      int num;
  293. {
  294.   register struct value_history_chunk *chunk;
  295.   register int i;
  296.   register int absnum = num;
  297.  
  298.   if (absnum <= 0)
  299.     absnum += value_history_count;
  300.  
  301.   if (absnum <= 0)
  302.     {
  303.       if (num == 0)
  304.     error ("The history is empty.");
  305.       else if (num == 1)
  306.     error ("There is only one value in the history.");
  307.       else
  308.     error ("History does not go back to $$%d.", -num);
  309.     }
  310.   if (absnum > value_history_count)
  311.     error ("History has not yet reached $%d.", absnum);
  312.  
  313.   absnum--;
  314.  
  315.   /* Now absnum is always absolute and origin zero.  */
  316.  
  317.   chunk = value_history_chain;
  318.   for (i = (value_history_count - 1) / VALUE_HISTORY_CHUNK - absnum / VALUE_HISTORY_CHUNK;
  319.        i > 0; i--)
  320.     chunk = chunk->next;
  321.  
  322.   return value_copy (chunk->values[absnum % VALUE_HISTORY_CHUNK]);
  323. }
  324.  
  325. /* Clear the value history entirely.
  326.    Must be done when new symbol tables are loaded,
  327.    because the type pointers become invalid.  */
  328.  
  329. void
  330. clear_value_history ()
  331. {
  332.   register struct value_history_chunk *next;
  333.   register int i;
  334.   register value val;
  335.  
  336.   while (value_history_chain)
  337.     {
  338.       for (i = 0; i < VALUE_HISTORY_CHUNK; i++)
  339.     if (val = value_history_chain->values[i])
  340.       free ((PTR)val);
  341.       next = value_history_chain->next;
  342.       free ((PTR)value_history_chain);
  343.       value_history_chain = next;
  344.     }
  345.   value_history_count = 0;
  346. }
  347.  
  348. static void
  349. show_values (num_exp, from_tty)
  350.      char *num_exp;
  351.      int from_tty;
  352. {
  353.   register int i;
  354.   register value val;
  355.   static int num = 1;
  356.  
  357.   if (num_exp)
  358.     {
  359.       if (num_exp[0] == '+' && num_exp[1] == '\0')
  360.     /* "info history +" should print from the stored position.  */
  361.     ;
  362.       else
  363.     /* "info history <exp>" should print around value number <exp>.  */
  364.     num = parse_and_eval_address (num_exp) - 5;
  365.     }
  366.   else
  367.     {
  368.       /* "info history" means print the last 10 values.  */
  369.       num = value_history_count - 9;
  370.     }
  371.  
  372.   if (num <= 0)
  373.     num = 1;
  374.  
  375.   for (i = num; i < num + 10 && i <= value_history_count; i++)
  376.     {
  377.       val = access_value_history (i);
  378.       printf_filtered ("$%d = ", i);
  379.       value_print (val, stdout, 0, Val_pretty_default);
  380.       printf_filtered ("\n");
  381.     }
  382.  
  383.   /* The next "info history +" should start after what we just printed.  */
  384.   num += 10;
  385.  
  386.   /* Hitting just return after this command should do the same thing as
  387.      "info history +".  If num_exp is null, this is unnecessary, since
  388.      "info history +" is not useful after "info history".  */
  389.   if (from_tty && num_exp)
  390.     {
  391.       num_exp[0] = '+';
  392.       num_exp[1] = '\0';
  393.     }
  394. }
  395.  
  396. /* Internal variables.  These are variables within the debugger
  397.    that hold values assigned by debugger commands.
  398.    The user refers to them with a '$' prefix
  399.    that does not appear in the variable names stored internally.  */
  400.  
  401. static struct internalvar *internalvars;
  402.  
  403. /* Look up an internal variable with name NAME.  NAME should not
  404.    normally include a dollar sign.
  405.  
  406.    If the specified internal variable does not exist,
  407.    one is created, with a void value.  */
  408.  
  409. struct internalvar *
  410. lookup_internalvar (name)
  411.      char *name;
  412. {
  413.   register struct internalvar *var;
  414.  
  415.   for (var = internalvars; var; var = var->next)
  416.     if (!strcmp (var->name, name))
  417.       return var;
  418.  
  419.   var = (struct internalvar *) xmalloc (sizeof (struct internalvar));
  420.   var->name = concat (name, NULL);
  421.   var->value = allocate_value (builtin_type_void);
  422.   release_value (var->value);
  423.   var->next = internalvars;
  424.   internalvars = var;
  425.   return var;
  426. }
  427.  
  428. value
  429. value_of_internalvar (var)
  430.      struct internalvar *var;
  431. {
  432.   register value val;
  433.  
  434. #ifdef IS_TRAPPED_INTERNALVAR
  435.   if (IS_TRAPPED_INTERNALVAR (var->name))
  436.     return VALUE_OF_TRAPPED_INTERNALVAR (var);
  437. #endif 
  438.  
  439.   val = value_copy (var->value);
  440.   if (VALUE_LAZY (val))
  441.     value_fetch_lazy (val);
  442.   VALUE_LVAL (val) = lval_internalvar;
  443.   VALUE_INTERNALVAR (val) = var;
  444.   return val;
  445. }
  446.  
  447. void
  448. set_internalvar_component (var, offset, bitpos, bitsize, newval)
  449.      struct internalvar *var;
  450.      int offset, bitpos, bitsize;
  451.      value newval;
  452. {
  453.   register char *addr = VALUE_CONTENTS (var->value) + offset;
  454.  
  455. #ifdef IS_TRAPPED_INTERNALVAR
  456.   if (IS_TRAPPED_INTERNALVAR (var->name))
  457.     SET_TRAPPED_INTERNALVAR (var, newval, bitpos, bitsize, offset);
  458. #endif
  459.  
  460.   if (bitsize)
  461.     modify_field (addr, (int) value_as_long (newval),
  462.           bitpos, bitsize);
  463.   else
  464.     bcopy (VALUE_CONTENTS (newval), addr,
  465.        TYPE_LENGTH (VALUE_TYPE (newval)));
  466. }
  467.  
  468. void
  469. set_internalvar (var, val)
  470.      struct internalvar *var;
  471.      value val;
  472. {
  473. #ifdef IS_TRAPPED_INTERNALVAR
  474.   if (IS_TRAPPED_INTERNALVAR (var->name))
  475.     SET_TRAPPED_INTERNALVAR (var, val, 0, 0, 0);
  476. #endif
  477.  
  478.   free ((PTR)var->value);
  479.   var->value = value_copy (val);
  480.   release_value (var->value);
  481. }
  482.  
  483. char *
  484. internalvar_name (var)
  485.      struct internalvar *var;
  486. {
  487.   return var->name;
  488. }
  489.  
  490. /* Free all internalvars.  Done when new symtabs are loaded,
  491.    because that makes the values invalid.  */
  492.  
  493. void
  494. clear_internalvars ()
  495. {
  496.   register struct internalvar *var;
  497.  
  498.   while (internalvars)
  499.     {
  500.       var = internalvars;
  501.       internalvars = var->next;
  502.       free ((PTR)var->name);
  503.       free ((PTR)var->value);
  504.       free ((PTR)var);
  505.     }
  506. }
  507.  
  508. static void
  509. show_convenience (ignore, from_tty)
  510.      char *ignore;
  511.      int from_tty;
  512. {
  513.   register struct internalvar *var;
  514.   int varseen = 0;
  515.  
  516.   for (var = internalvars; var; var = var->next)
  517.     {
  518. #ifdef IS_TRAPPED_INTERNALVAR
  519.       if (IS_TRAPPED_INTERNALVAR (var->name))
  520.     continue;
  521. #endif
  522.       if (!varseen)
  523.     {
  524.       varseen = 1;
  525.     }
  526.       printf_filtered ("$%s = ", var->name);
  527.       value_print (var->value, stdout, 0, Val_pretty_default);
  528.       printf_filtered ("\n");
  529.     }
  530.   if (!varseen)
  531.     printf ("No debugger convenience variables now defined.\n\
  532. Convenience variables have names starting with \"$\";\n\
  533. use \"set\" as in \"set $foo = 5\" to define them.\n");
  534. }
  535.  
  536. /* Extract a value as a C number (either long or double).
  537.    Knows how to convert fixed values to double, or
  538.    floating values to long.
  539.    Does not deallocate the value.  */
  540.  
  541. LONGEST
  542. value_as_long (val)
  543.      register value val;
  544. {
  545.   /* This coerces arrays and functions, which is necessary (e.g.
  546.      in disassemble_command).  It also dereferences references, which
  547.      I suspect is the most logical thing to do.  */
  548.   if (TYPE_CODE (VALUE_TYPE (val)) != TYPE_CODE_ENUM)
  549.     COERCE_ARRAY (val);
  550.   return unpack_long (VALUE_TYPE (val), VALUE_CONTENTS (val));
  551. }
  552.  
  553. double
  554. value_as_double (val)
  555.      register value val;
  556. {
  557.   double foo;
  558.   int inv;
  559.   
  560.   foo = unpack_double (VALUE_TYPE (val), VALUE_CONTENTS (val), &inv);
  561.   if (inv)
  562.     error ("Invalid floating value found in program.");
  563.   return foo;
  564. }
  565. /* Extract a value as a C pointer.
  566.    Does not deallocate the value.  */
  567. CORE_ADDR
  568. value_as_pointer (val)
  569.      value val;
  570. {
  571.   /* Assume a CORE_ADDR can fit in a LONGEST (for now).  Not sure
  572.      whether we want this to be true eventually.  */
  573.   return value_as_long (val);
  574. }
  575.  
  576. /* Unpack raw data (copied from debugee, target byte order) at VALADDR
  577.    as a long, or as a double, assuming the raw data is described
  578.    by type TYPE.  Knows how to convert different sizes of values
  579.    and can convert between fixed and floating point.  We don't assume
  580.    any alignment for the raw data.  Return value is in host byte order.
  581.  
  582.    If you want functions and arrays to be coerced to pointers, and
  583.    references to be dereferenced, call value_as_long() instead.
  584.  
  585.    C++: It is assumed that the front-end has taken care of
  586.    all matters concerning pointers to members.  A pointer
  587.    to member which reaches here is considered to be equivalent
  588.    to an INT (or some size).  After all, it is only an offset.  */
  589.  
  590. /* FIXME:  This should be rewritten as a switch statement for speed and
  591.    ease of comprehension.  */
  592.  
  593. LONGEST
  594. unpack_long (type, valaddr)
  595.      struct type *type;
  596.      char *valaddr;
  597. {
  598.   register enum type_code code = TYPE_CODE (type);
  599.   register int len = TYPE_LENGTH (type);
  600.   register int nosign = TYPE_UNSIGNED (type);
  601.  
  602. #ifdef KGDB
  603.   union { 
  604.    double    foo; /* double forces alignment. */
  605.    char    buffer[8];
  606.   } tmpMem;
  607.   bcopy(valaddr,tmpMem.buffer,8);
  608.   valaddr = tmpMem.buffer;
  609. #endif
  610.  
  611.   if (code == TYPE_CODE_ENUM || code == TYPE_CODE_BOOL)
  612.     code = TYPE_CODE_INT;
  613.   if (code == TYPE_CODE_FLT)
  614.     {
  615.       if (len == sizeof (float))
  616.     {
  617.       float retval;
  618.       bcopy (valaddr, &retval, sizeof (retval));
  619.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  620.       return retval;
  621.     }
  622.  
  623.       if (len == sizeof (double))
  624.     {
  625.       double retval;
  626.       bcopy (valaddr, &retval, sizeof (retval));
  627.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  628.       return retval;
  629.     }
  630.       else
  631.     {
  632.       error ("Unexpected type of floating point number.");
  633.     }
  634.     }
  635.   else if (code == TYPE_CODE_INT && nosign)
  636.     {
  637.       if (len == sizeof (char))
  638.     {
  639.       unsigned char retval = * (unsigned char *) valaddr;
  640.       /* SWAP_TARGET_AND_HOST (&retval, sizeof (unsigned char)); */
  641.       return retval;
  642.     }
  643.  
  644.       if (len == sizeof (short))
  645.     {
  646.       unsigned short retval;
  647.       bcopy (valaddr, &retval, sizeof (retval));
  648.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  649.       return retval;
  650.     }
  651.  
  652.       if (len == sizeof (int))
  653.     {
  654.       unsigned int retval;
  655.       bcopy (valaddr, &retval, sizeof (retval));
  656.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  657.       return retval;
  658.     }
  659.  
  660.       if (len == sizeof (long))
  661.     {
  662.       unsigned long retval;
  663.       bcopy (valaddr, &retval, sizeof (retval));
  664.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  665.       return retval;
  666.     }
  667. #ifdef LONG_LONG
  668.       if (len == sizeof (long long))
  669.     {
  670.       unsigned long long retval;
  671.       bcopy (valaddr, &retval, sizeof (retval));
  672.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  673.       return retval;
  674.     }
  675. #endif
  676.       else
  677.     {
  678.       error ("That operation is not possible on an integer of that size.");
  679.     }
  680.     }
  681.   else if (code == TYPE_CODE_INT)
  682.     {
  683.       if (len == sizeof (char))
  684.     {
  685.       SIGNED char retval;    /* plain chars might be unsigned on host */
  686.       bcopy (valaddr, &retval, sizeof (retval));
  687.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  688.       return retval;
  689.     }
  690.  
  691.       if (len == sizeof (short))
  692.     {
  693.       short retval;
  694.       bcopy (valaddr, &retval, sizeof (retval));
  695.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  696.       return retval;
  697.     }
  698.  
  699.       if (len == sizeof (int))
  700.     {
  701.       int retval;
  702.       bcopy (valaddr, &retval, sizeof (retval));
  703.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  704.       return retval;
  705.     }
  706.  
  707.       if (len == sizeof (long))
  708.     {
  709.       long retval;
  710.       bcopy (valaddr, &retval, sizeof (retval));
  711.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  712.       return retval;
  713.     }
  714.  
  715. #ifdef LONG_LONG
  716.       if (len == sizeof (long long))
  717.     {
  718.       long long retval;
  719.       bcopy (valaddr, &retval, sizeof (retval));
  720.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  721.       return retval;
  722.     }
  723. #endif
  724.       else
  725.     {
  726.       error ("That operation is not possible on an integer of that size.");
  727.     }
  728.     }
  729.   /* Assume a CORE_ADDR can fit in a LONGEST (for now).  Not sure
  730.      whether we want this to be true eventually.  */
  731.   else if (code == TYPE_CODE_PTR
  732.        || code == TYPE_CODE_REF)
  733.     {
  734.       if (len == sizeof(long))
  735.       {
  736.     long retval;
  737.     bcopy (valaddr, &retval, sizeof(retval));
  738.     SWAP_TARGET_AND_HOST (&retval, sizeof(retval));
  739.     return retval;
  740.       }
  741.       else if (len == sizeof(short))
  742.       {
  743.     short retval;
  744.     bcopy (valaddr, &retval, len);
  745.     SWAP_TARGET_AND_HOST (&retval, len);
  746.     return retval;
  747.       }
  748.     }
  749.   else if (code == TYPE_CODE_MEMBER)
  750.     error ("not implemented: member types in unpack_long");
  751.   else if (code == TYPE_CODE_CHAR)
  752.     return *(unsigned char *)valaddr;
  753.  
  754.   error ("Value not integer or pointer.");
  755.   return 0;     /* For lint -- never reached */
  756. }
  757.  
  758. /* Return a double value from the specified type and address.
  759.    INVP points to an int which is set to 0 for valid value,
  760.    1 for invalid value (bad float format).  In either case,
  761.    the returned double is OK to use.  Argument is in target
  762.    format, result is in host format.  */
  763.  
  764. double
  765. unpack_double (type, valaddr, invp)
  766.      struct type *type;
  767.      char *valaddr;
  768.      int *invp;
  769. {
  770.   register enum type_code code = TYPE_CODE (type);
  771.   register int len = TYPE_LENGTH (type);
  772.   register int nosign = TYPE_UNSIGNED (type);
  773.  
  774. #ifdef KGDB
  775.   union { 
  776.    double    foo; /* double forces alignment. */
  777.    char    buffer[8];
  778.   } tmpMem;
  779.   bcopy(valaddr,tmpMem.buffer,8);
  780.   valaddr = tmpMem.buffer;
  781. #endif
  782.  
  783.   *invp = 0;            /* Assume valid.   */
  784.   if (code == TYPE_CODE_FLT)
  785.     {
  786.       if (INVALID_FLOAT (valaddr, len))
  787.     {
  788.       *invp = 1;
  789.       return 1.234567891011121314;
  790.     }
  791.  
  792.       if (len == sizeof (float))
  793.     {
  794.       float retval;
  795.       bcopy (valaddr, &retval, sizeof (retval));
  796.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  797.       return retval;
  798.     }
  799.  
  800.       if (len == sizeof (double))
  801.     {
  802.       double retval;
  803.       bcopy (valaddr, &retval, sizeof (retval));
  804.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  805.       return retval;
  806.     }
  807.       else
  808.     {
  809.       error ("Unexpected type of floating point number.");
  810.       return 0; /* Placate lint.  */
  811.     }
  812.     }
  813.   else if (nosign) {
  814.    /* Unsigned -- be sure we compensate for signed LONGEST.  */
  815. #ifdef LONG_LONG
  816.    return (unsigned long long) unpack_long (type, valaddr);
  817. #else
  818.    return (unsigned long     ) unpack_long (type, valaddr);
  819. #endif
  820.   } else {
  821.     /* Signed -- we are OK with unpack_long.  */
  822.     return unpack_long (type, valaddr);
  823.   }
  824. }
  825.  
  826. /* Unpack raw data (copied from debugee, target byte order) at VALADDR
  827.    as a CORE_ADDR, assuming the raw data is described by type TYPE.
  828.    We don't assume any alignment for the raw data.  Return value is in
  829.    host byte order.
  830.  
  831.    If you want functions and arrays to be coerced to pointers, and
  832.    references to be dereferenced, call value_as_pointer() instead.
  833.  
  834.    C++: It is assumed that the front-end has taken care of
  835.    all matters concerning pointers to members.  A pointer
  836.    to member which reaches here is considered to be equivalent
  837.    to an INT (or some size).  After all, it is only an offset.  */
  838.  
  839. CORE_ADDR
  840. unpack_pointer (type, valaddr)
  841.      struct type *type;
  842.      char *valaddr;
  843. {
  844. #if 0
  845.   /* The user should be able to use an int (e.g. 0x7892) in contexts
  846.      where a pointer is expected.  So this doesn't do enough.  */
  847.   register enum type_code code = TYPE_CODE (type);
  848.   register int len = TYPE_LENGTH (type);
  849.  
  850.   if (code == TYPE_CODE_PTR
  851.       || code == TYPE_CODE_REF)
  852.     {
  853.       if (len == sizeof (CORE_ADDR))
  854.     {
  855.       CORE_ADDR retval;
  856.       bcopy (valaddr, &retval, sizeof (retval));
  857.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  858.       return retval;
  859.     }
  860.       error ("Unrecognized pointer size.");
  861.     }
  862.   else if (code == TYPE_CODE_MEMBER)
  863.     error ("not implemented: member types in unpack_pointer");
  864.  
  865.   error ("Value is not a pointer.");
  866.   return 0;     /* For lint -- never reached */
  867. #else
  868.   /* Assume a CORE_ADDR can fit in a LONGEST (for now).  Not sure
  869.      whether we want this to be true eventually.  */
  870.   return unpack_long (type, valaddr);
  871. #endif
  872. }
  873.  
  874. /* Given a value ARG1 (offset by OFFSET bytes)
  875.    of a struct or union type ARG_TYPE,
  876.    extract and return the value of one of its fields.
  877.    FIELDNO says which field.
  878.  
  879.    For C++, must also be able to return values from static fields */
  880.  
  881. value
  882. value_primitive_field (arg1, offset, fieldno, arg_type)
  883.      register value arg1;
  884.      int offset;
  885.      register int fieldno;
  886.      register struct type *arg_type;
  887. {
  888.   register value v;
  889.   register struct type *type;
  890.  
  891.   check_stub_type (arg_type);
  892.   type = TYPE_FIELD_TYPE (arg_type, fieldno);
  893.  
  894.   /* Handle packed fields */
  895.  
  896.   offset += TYPE_FIELD_BITPOS (arg_type, fieldno) / 8;
  897.   if (TYPE_FIELD_BITSIZE (arg_type, fieldno))
  898.     {
  899.       v = value_from_longest (type,
  900.                unpack_field_as_long (arg_type,
  901.                          VALUE_CONTENTS (arg1),
  902.                          fieldno));
  903.       VALUE_BITPOS (v) = TYPE_FIELD_BITPOS (arg_type, fieldno) % 8;
  904.       VALUE_BITSIZE (v) = TYPE_FIELD_BITSIZE (arg_type, fieldno);
  905.     }
  906.   else
  907.     {
  908.       v = allocate_value (type);
  909.       if (VALUE_LAZY (arg1))
  910.     VALUE_LAZY (v) = 1;
  911.       else
  912.     bcopy (VALUE_CONTENTS_RAW (arg1) + offset,
  913.            VALUE_CONTENTS_RAW (v),
  914.            TYPE_LENGTH (type));
  915.     }
  916.   VALUE_LVAL (v) = VALUE_LVAL (arg1);
  917.   if (VALUE_LVAL (arg1) == lval_internalvar)
  918.     VALUE_LVAL (v) = lval_internalvar_component;
  919.   VALUE_ADDRESS (v) = VALUE_ADDRESS (arg1);
  920.   VALUE_OFFSET (v) = offset + VALUE_OFFSET (arg1);
  921.   return v;
  922. }
  923.  
  924. /* Given a value ARG1 of a struct or union type,
  925.    extract and return the value of one of its fields.
  926.    FIELDNO says which field.
  927.  
  928.    For C++, must also be able to return values from static fields */
  929.  
  930. value
  931. value_field (arg1, fieldno)
  932.      register value arg1;
  933.      register int fieldno;
  934. {
  935.   return value_primitive_field (arg1, 0, fieldno, VALUE_TYPE (arg1));
  936. }
  937.  
  938. /* Return a non-virtual function as a value.
  939.    F is the list of member functions which contains the desired method.
  940.    J is an index into F which provides the desired method. */
  941.  
  942. value
  943. value_fn_field (f, j)
  944.      struct fn_field *f;
  945.      int j;
  946. {
  947.   register value v;
  948.   register struct type *type = TYPE_FN_FIELD_TYPE (f, j);
  949.   struct symbol *sym;
  950.  
  951.   sym = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
  952.                0, VAR_NAMESPACE, 0, NULL);
  953.   if (! sym) error ("Internal error: could not find physical method named %s",
  954.             TYPE_FN_FIELD_PHYSNAME (f, j));
  955.   
  956.   v = allocate_value (type);
  957.   VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
  958.   VALUE_TYPE (v) = type;
  959.   return v;
  960. }
  961.  
  962. /* Return a virtual function as a value.
  963.    ARG1 is the object which provides the virtual function
  964.    table pointer.  ARG1 is side-effected in calling this function.
  965.    F is the list of member functions which contains the desired virtual
  966.    function.
  967.    J is an index into F which provides the desired virtual function.
  968.  
  969.    TYPE is the type in which F is located.  */
  970. value
  971. value_virtual_fn_field (arg1, f, j, type)
  972.      value arg1;
  973.      struct fn_field *f;
  974.      int j;
  975.      struct type *type;
  976. {
  977.   /* First, get the virtual function table pointer.  That comes
  978.      with a strange type, so cast it to type `pointer to long' (which
  979.      should serve just fine as a function type).  Then, index into
  980.      the table, and convert final value to appropriate function type.  */
  981.   value entry, vfn, vtbl;
  982.   value vi = value_from_longest (builtin_type_int, 
  983.                   (LONGEST) TYPE_FN_FIELD_VOFFSET (f, j));
  984.   struct type *fcontext = TYPE_FN_FIELD_FCONTEXT (f, j);
  985.   struct type *context;
  986.   if (fcontext == NULL)
  987.    /* We don't have an fcontext (e.g. the program was compiled with
  988.       g++ version 1).  Try to get the vtbl from the TYPE_VPTR_BASETYPE.
  989.       This won't work right for multiple inheritance, but at least we
  990.       should do as well as GDB 3.x did.  */
  991.     fcontext = TYPE_VPTR_BASETYPE (type);
  992.   context = lookup_pointer_type (fcontext);
  993.   /* Now context is a pointer to the basetype containing the vtbl.  */
  994.   if (TYPE_TARGET_TYPE (context) != VALUE_TYPE (arg1))
  995.     arg1 = value_ind (value_cast (context, value_addr (arg1)));
  996.  
  997.   context = VALUE_TYPE (arg1);
  998.   /* Now context is the basetype containing the vtbl.  */
  999.  
  1000.   /* This type may have been defined before its virtual function table
  1001.      was.  If so, fill in the virtual function table entry for the
  1002.      type now.  */
  1003.   if (TYPE_VPTR_FIELDNO (context) < 0)
  1004.     fill_in_vptr_fieldno (context);
  1005.  
  1006.   /* The virtual function table is now an array of structures
  1007.      which have the form { int16 offset, delta; void *pfn; }.  */
  1008.   vtbl = value_ind (value_field (arg1, TYPE_VPTR_FIELDNO (context)));
  1009.  
  1010.   /* Index into the virtual function table.  This is hard-coded because
  1011.      looking up a field is not cheap, and it may be important to save
  1012.      time, e.g. if the user has set a conditional breakpoint calling
  1013.      a virtual function.  */
  1014.   entry = value_subscript (vtbl, vi);
  1015.  
  1016.   /* Move the `this' pointer according to the virtual function table.  */
  1017.   VALUE_OFFSET (arg1) += value_as_long (value_field (entry, 0));
  1018.   if (! VALUE_LAZY (arg1))
  1019.     {
  1020.       VALUE_LAZY (arg1) = 1;
  1021.       value_fetch_lazy (arg1);
  1022.     }
  1023.  
  1024.   vfn = value_field (entry, 2);
  1025.   /* Reinstantiate the function pointer with the correct type.  */
  1026.   VALUE_TYPE (vfn) = lookup_pointer_type (TYPE_FN_FIELD_TYPE (f, j));
  1027.  
  1028.   return vfn;
  1029. }
  1030.  
  1031. /* ARG is a pointer to an object we know to be at least
  1032.    a DTYPE.  BTYPE is the most derived basetype that has
  1033.    already been searched (and need not be searched again).
  1034.    After looking at the vtables between BTYPE and DTYPE,
  1035.    return the most derived type we find.  The caller must
  1036.    be satisfied when the return value == DTYPE.
  1037.  
  1038.    FIXME-tiemann: should work with dossier entries as well.  */
  1039.  
  1040. static value
  1041. value_headof (arg, btype, dtype)
  1042.      value arg;
  1043.      struct type *btype, *dtype;
  1044. {
  1045.   /* First collect the vtables we must look at for this object.  */
  1046.   /* FIXME-tiemann: right now, just look at top-most vtable.  */
  1047.   value vtbl, entry, best_entry = 0;
  1048.   /* FIXME: entry_type is never used.  */
  1049.   struct type *entry_type;
  1050.   int i, nelems;
  1051.   int offset, best_offset = 0;
  1052.   struct symbol *sym;
  1053.   CORE_ADDR pc_for_sym;
  1054.   char *demangled_name;
  1055.   struct minimal_symbol *msymbol;
  1056.  
  1057.   btype = TYPE_VPTR_BASETYPE (dtype);
  1058.   check_stub_type (btype);
  1059.   if (btype != dtype)
  1060.     vtbl = value_cast (lookup_pointer_type (btype), arg);
  1061.   else
  1062.     vtbl = arg;
  1063.   vtbl = value_ind (value_field (value_ind (vtbl), TYPE_VPTR_FIELDNO (btype)));
  1064.  
  1065.   /* Check that VTBL looks like it points to a virtual function table.  */
  1066.   msymbol = lookup_minimal_symbol_by_pc (VALUE_ADDRESS (vtbl));
  1067.   if (msymbol == NULL
  1068.       || !VTBL_PREFIX_P (demangled_name = msymbol -> name))
  1069.     {
  1070.       /* If we expected to find a vtable, but did not, let the user
  1071.      know that we aren't happy, but don't throw an error.
  1072.      FIXME: there has to be a better way to do this.  */
  1073.       struct type *error_type = (struct type *)xmalloc (sizeof (struct type));
  1074.       bcopy (VALUE_TYPE (arg), error_type, sizeof (struct type));
  1075.       TYPE_NAME (error_type) = savestring ("suspicious *", sizeof ("suspicious *"));
  1076.       VALUE_TYPE (arg) = error_type;
  1077.       return arg;
  1078.     }
  1079.  
  1080.   /* Now search through the virtual function table.  */
  1081.   entry = value_ind (vtbl);
  1082.   nelems = longest_to_int (value_as_long (value_field (entry, 2)));
  1083.   for (i = 1; i <= nelems; i++)
  1084.     {
  1085.       entry = value_subscript (vtbl, value_from_longest (builtin_type_int, 
  1086.                               (LONGEST) i));
  1087.       offset = longest_to_int (value_as_long (value_field (entry, 0)));
  1088.       /* If we use '<=' we can handle single inheritance
  1089.        * where all offsets are zero - just use the first entry found. */
  1090.       if (offset <= best_offset)
  1091.     {
  1092.       best_offset = offset;
  1093.       best_entry = entry;
  1094.     }
  1095.     }
  1096.   /* Move the pointer according to BEST_ENTRY's offset, and figure
  1097.      out what type we should return as the new pointer.  */
  1098.   if (best_entry == 0)
  1099.     {
  1100.       /* An alternative method (which should no longer be necessary).
  1101.        * But we leave it in for future use, when we will hopefully
  1102.        * have optimizes the vtable to use thunks instead of offsets. */
  1103.       /* Use the name of vtable itself to extract a base type. */
  1104.       demangled_name += 4;  /* Skip _vt$ prefix. */
  1105.     }
  1106.   else
  1107.     {
  1108.       pc_for_sym = value_as_pointer (value_field (best_entry, 2));
  1109.       sym = find_pc_function (pc_for_sym);
  1110.       demangled_name = cplus_demangle (SYMBOL_NAME (sym), -1);
  1111.       *(strchr (demangled_name, ':')) = '\0';
  1112.     }
  1113.   sym = lookup_symbol (demangled_name, 0, VAR_NAMESPACE, 0, 0);
  1114.   if (sym == 0)
  1115.     error ("could not find type declaration for `%s'", SYMBOL_NAME (sym));
  1116.   if (best_entry)
  1117.     {
  1118.       free (demangled_name);
  1119.       arg = value_add (value_cast (builtin_type_int, arg),
  1120.                value_field (best_entry, 0));
  1121.     }
  1122.   VALUE_TYPE (arg) = lookup_pointer_type (SYMBOL_TYPE (sym));
  1123.   return arg;
  1124. }
  1125.  
  1126. /* ARG is a pointer object of type TYPE.  If TYPE has virtual
  1127.    function tables, probe ARG's tables (including the vtables
  1128.    of its baseclasses) to figure out the most derived type that ARG
  1129.    could actually be a pointer to.  */
  1130.  
  1131. value
  1132. value_from_vtable_info (arg, type)
  1133.      value arg;
  1134.      struct type *type;
  1135. {
  1136.   /* Take care of preliminaries.  */
  1137.   if (TYPE_VPTR_FIELDNO (type) < 0)
  1138.     fill_in_vptr_fieldno (type);
  1139.   if (TYPE_VPTR_FIELDNO (type) < 0 || VALUE_REPEATED (arg))
  1140.     return 0;
  1141.  
  1142.   return value_headof (arg, 0, type);
  1143. }
  1144.  
  1145. /* Compute the address of the baseclass which is
  1146.    the INDEXth baseclass of class TYPE.  The TYPE base
  1147.    of the object is at VALADDR.
  1148.  
  1149.    If ERRP is non-NULL, set *ERRP to be the errno code of any error,
  1150.    or 0 if no error.  In that case the return value is not the address
  1151.    of the baseclasss, but the address which could not be read
  1152.    successfully.  */
  1153.  
  1154. char *
  1155. baseclass_addr (type, index, valaddr, valuep, errp)
  1156.      struct type *type;
  1157.      int index;
  1158.      char *valaddr;
  1159.      value *valuep;
  1160.      int *errp;
  1161. {
  1162.   struct type *basetype = TYPE_BASECLASS (type, index);
  1163.  
  1164.   if (errp)
  1165.     *errp = 0;
  1166.  
  1167.   if (BASETYPE_VIA_VIRTUAL (type, index))
  1168.     {
  1169.       /* Must hunt for the pointer to this virtual baseclass.  */
  1170.       register int i, len = TYPE_NFIELDS (type);
  1171.       register int n_baseclasses = TYPE_N_BASECLASSES (type);
  1172.       char *vbase_name, *type_name = type_name_no_tag (basetype);
  1173.  
  1174.       vbase_name = (char *)alloca (strlen (type_name) + 8);
  1175.       sprintf (vbase_name, "_vb$%s", type_name);
  1176.       /* First look for the virtual baseclass pointer
  1177.      in the fields.  */
  1178.       for (i = n_baseclasses; i < len; i++)
  1179.     {
  1180.       if (! strcmp (vbase_name, TYPE_FIELD_NAME (type, i)))
  1181.         {
  1182.           value val = allocate_value (basetype);
  1183.           CORE_ADDR addr;
  1184.           int status;
  1185.  
  1186.           addr
  1187.         = unpack_pointer (TYPE_FIELD_TYPE (type, i),
  1188.                   valaddr + (TYPE_FIELD_BITPOS (type, i) / 8));
  1189.  
  1190.           status = target_read_memory (addr,
  1191.                        VALUE_CONTENTS_RAW (val),
  1192.                        TYPE_LENGTH (basetype));
  1193.           VALUE_LVAL (val) = lval_memory;
  1194.           VALUE_ADDRESS (val) = addr;
  1195.  
  1196.           if (status != 0)
  1197.         {
  1198.           if (valuep)
  1199.             *valuep = NULL;
  1200.           release_value (val);
  1201.           value_free (val);
  1202.           if (errp)
  1203.             *errp = status;
  1204.           return (char *)addr;
  1205.         }
  1206.           else
  1207.         {
  1208.           if (valuep)
  1209.             *valuep = val;
  1210.           return (char *) VALUE_CONTENTS (val);
  1211.         }
  1212.         }
  1213.     }
  1214.       /* Not in the fields, so try looking through the baseclasses.  */
  1215.       for (i = index+1; i < n_baseclasses; i++)
  1216.     {
  1217.       char *baddr;
  1218.  
  1219.       baddr = baseclass_addr (type, i, valaddr, valuep, errp);
  1220.       if (baddr)
  1221.         return baddr;
  1222.     }
  1223.       /* Not found.  */
  1224.       if (valuep)
  1225.     *valuep = 0;
  1226.       return 0;
  1227.     }
  1228.  
  1229.   /* Baseclass is easily computed.  */
  1230.   if (valuep)
  1231.     *valuep = 0;
  1232.   return valaddr + TYPE_BASECLASS_BITPOS (type, index) / 8;
  1233. }
  1234.  
  1235. long
  1236. unpack_field_as_long (type, valaddr, fieldno)
  1237.      struct type *type;
  1238.      char *valaddr;
  1239.      int fieldno;
  1240. {
  1241.   unsigned long val;
  1242.   int bitpos = TYPE_FIELD_BITPOS (type, fieldno);
  1243.   int bitsize = TYPE_FIELD_BITSIZE (type, fieldno);
  1244.  
  1245.   bcopy (valaddr + bitpos / 8, &val, sizeof val);
  1246.   SWAP_TARGET_AND_HOST (&val, sizeof val);
  1247.  
  1248.   /* Extracting bits depends on endianness of the machine.  */
  1249. #if BITS_BIG_ENDIAN
  1250.   val = val >> (sizeof val * 8 - bitpos % 8 - bitsize);
  1251. #else
  1252.   val = val >> (bitpos % 8);
  1253. #endif
  1254.  
  1255.   if (bitsize < 8 * sizeof (val))
  1256.     val &= (((unsigned long)1) << bitsize) - 1;
  1257.   return val;
  1258. }
  1259.  
  1260. /* Modify the value of a bitfield.  ADDR points to a block of memory in
  1261.    target byte order; the bitfield starts in the byte pointed to.  FIELDVAL
  1262.    is the desired value of the field, in host byte order.  BITPOS and BITSIZE
  1263.    indicate which bits (in target bit order) comprise the bitfield.  */
  1264.  
  1265. void
  1266. modify_field (addr, fieldval, bitpos, bitsize)
  1267.      char *addr;
  1268.      int fieldval;
  1269.      int bitpos, bitsize;
  1270. {
  1271.   long oword;
  1272.  
  1273.   /* Reject values too big to fit in the field in question,
  1274.      otherwise adjoining fields may be corrupted.  */
  1275.   if (bitsize < (8 * sizeof (fieldval))
  1276.       && 0 != (fieldval & ~((1<<bitsize)-1)))
  1277.     error ("Value %d does not fit in %d bits.", fieldval, bitsize);
  1278.   
  1279.   bcopy (addr, &oword, sizeof oword);
  1280.   SWAP_TARGET_AND_HOST (&oword, sizeof oword);        /* To host format */
  1281.  
  1282.   /* Shifting for bit field depends on endianness of the target machine.  */
  1283. #if BITS_BIG_ENDIAN
  1284.   bitpos = sizeof (oword) * 8 - bitpos - bitsize;
  1285. #endif
  1286.  
  1287.   /* Mask out old value, while avoiding shifts >= longword size */
  1288.   if (bitsize < 8 * sizeof (oword))
  1289.     oword &= ~(((((unsigned long)1) << bitsize) - 1) << bitpos);
  1290.   else
  1291.     oword &= ~((-1) << bitpos);
  1292.   oword |= fieldval << bitpos;
  1293.  
  1294.   SWAP_TARGET_AND_HOST (&oword, sizeof oword);        /* To target format */
  1295.   bcopy (&oword, addr, sizeof oword);
  1296. }
  1297.  
  1298. /* Convert C numbers into newly allocated values */
  1299.  
  1300. value
  1301. value_from_longest (type, num)
  1302.      struct type *type;
  1303.      register LONGEST num;
  1304. {
  1305.   register value val = allocate_value (type);
  1306.   register enum type_code code = TYPE_CODE (type);
  1307.   register int len = TYPE_LENGTH (type);
  1308.  
  1309.   /* FIXME, we assume that pointers have the same form and byte order as
  1310.      integers, and that all pointers have the same form.  */
  1311.   if (code == TYPE_CODE_INT  || code == TYPE_CODE_ENUM || 
  1312.       code == TYPE_CODE_CHAR || code == TYPE_CODE_PTR ||
  1313.       code == TYPE_CODE_REF)
  1314.     {
  1315.       if (len == sizeof (char))
  1316.     * (char *) VALUE_CONTENTS_RAW (val) = num;
  1317.       else if (len == sizeof (short))
  1318.     * (short *) VALUE_CONTENTS_RAW (val) = num;
  1319.       else if (len == sizeof (int))
  1320.     * (int *) VALUE_CONTENTS_RAW (val) = num;
  1321.       else if (len == sizeof (long))
  1322.     * (long *) VALUE_CONTENTS_RAW (val) = num;
  1323. #ifdef LONG_LONG
  1324.       else if (len == sizeof (long long))
  1325.     * (long long *) VALUE_CONTENTS_RAW (val) = num;
  1326. #endif
  1327.       else
  1328.     error ("Integer type encountered with unexpected data length.");
  1329.     }
  1330.   else
  1331.     error ("Unexpected type encountered for integer constant.");
  1332.  
  1333.   /* num was in host byte order.  So now put the value's contents
  1334.      into target byte order.  */
  1335.   SWAP_TARGET_AND_HOST (VALUE_CONTENTS_RAW (val), len);
  1336.  
  1337.   return val;
  1338. }
  1339.  
  1340. value
  1341. value_from_double (type, num)
  1342.      struct type *type;
  1343.      double num;
  1344. {
  1345.   register value val = allocate_value (type);
  1346.   register enum type_code code = TYPE_CODE (type);
  1347.   register int len = TYPE_LENGTH (type);
  1348.  
  1349.   if (code == TYPE_CODE_FLT)
  1350.     {
  1351.       if (len == sizeof (float))
  1352.     * (float *) VALUE_CONTENTS_RAW (val) = num;
  1353.       else if (len == sizeof (double))
  1354.     * (double *) VALUE_CONTENTS_RAW (val) = num;
  1355.       else
  1356.     error ("Floating type encountered with unexpected data length.");
  1357.     }
  1358.   else
  1359.     error ("Unexpected type encountered for floating constant.");
  1360.  
  1361.   /* num was in host byte order.  So now put the value's contents
  1362.      into target byte order.  */
  1363.   SWAP_TARGET_AND_HOST (VALUE_CONTENTS_RAW (val), len);
  1364.  
  1365.   return val;
  1366. }
  1367.  
  1368. /* Deal with the value that is "about to be returned".  */
  1369.  
  1370. /* Return the value that a function returning now
  1371.    would be returning to its caller, assuming its type is VALTYPE.
  1372.    RETBUF is where we look for what ought to be the contents
  1373.    of the registers (in raw form).  This is because it is often
  1374.    desirable to restore old values to those registers
  1375.    after saving the contents of interest, and then call
  1376.    this function using the saved values.
  1377.    struct_return is non-zero when the function in question is
  1378.    using the structure return conventions on the machine in question;
  1379.    0 when it is using the value returning conventions (this often
  1380.    means returning pointer to where structure is vs. returning value). */
  1381.  
  1382. value
  1383. value_being_returned (valtype, retbuf, struct_return)
  1384.      register struct type *valtype;
  1385.      char retbuf[REGISTER_BYTES];
  1386.      int struct_return;
  1387.      /*ARGSUSED*/
  1388. {
  1389.   register value val;
  1390.   CORE_ADDR addr;
  1391.  
  1392. #if defined (EXTRACT_STRUCT_VALUE_ADDRESS)
  1393.   /* If this is not defined, just use EXTRACT_RETURN_VALUE instead.  */
  1394.   if (struct_return) {
  1395.     addr = EXTRACT_STRUCT_VALUE_ADDRESS (retbuf);
  1396.     if (!addr)
  1397.       error ("Function return value unknown");
  1398.     return value_at (valtype, addr);
  1399.   }
  1400. #endif
  1401.  
  1402.   val = allocate_value (valtype);
  1403.   EXTRACT_RETURN_VALUE (valtype, retbuf, VALUE_CONTENTS_RAW (val));
  1404.  
  1405.   return val;
  1406. }
  1407.  
  1408. /* Should we use EXTRACT_STRUCT_VALUE_ADDRESS instead of
  1409.    EXTRACT_RETURN_VALUE?  GCC_P is true if compiled with gcc
  1410.    and TYPE is the type (which is known to be struct, union or array).
  1411.  
  1412.    On most machines, the struct convention is used unless we are
  1413.    using gcc and the type is of a special size.  */
  1414. #if !defined (USE_STRUCT_CONVENTION)
  1415. #define USE_STRUCT_CONVENTION(gcc_p, type)\
  1416.   (!((gcc_p) && (TYPE_LENGTH (value_type) == 1                \
  1417.          || TYPE_LENGTH (value_type) == 2             \
  1418.              || TYPE_LENGTH (value_type) == 4             \
  1419.          || TYPE_LENGTH (value_type) == 8             \
  1420.          )                                            \
  1421.      ))
  1422. #endif
  1423.  
  1424. /* Return true if the function specified is using the structure returning
  1425.    convention on this machine to return arguments, or 0 if it is using
  1426.    the value returning convention.  FUNCTION is the value representing
  1427.    the function, FUNCADDR is the address of the function, and VALUE_TYPE
  1428.    is the type returned by the function.  GCC_P is nonzero if compiled
  1429.    with GCC.  */
  1430.  
  1431. int
  1432. using_struct_return (function, funcaddr, value_type, gcc_p)
  1433.      value function;
  1434.      CORE_ADDR funcaddr;
  1435.      struct type *value_type;
  1436.      int gcc_p;
  1437.      /*ARGSUSED*/
  1438. {
  1439.   register enum type_code code = TYPE_CODE (value_type);
  1440.  
  1441.   if (code == TYPE_CODE_ERROR)
  1442.     error ("Function return type unknown.");
  1443.  
  1444.   if (code == TYPE_CODE_STRUCT ||
  1445.       code == TYPE_CODE_UNION ||
  1446.       code == TYPE_CODE_ARRAY)
  1447.     return USE_STRUCT_CONVENTION (gcc_p, value_type);
  1448.  
  1449.   return 0;
  1450. }
  1451.  
  1452. /* Store VAL so it will be returned if a function returns now.
  1453.    Does not verify that VAL's type matches what the current
  1454.    function wants to return.  */
  1455.  
  1456. void
  1457. set_return_value (val)
  1458.      value val;
  1459. {
  1460.   register enum type_code code = TYPE_CODE (VALUE_TYPE (val));
  1461.   double dbuf;
  1462.   LONGEST lbuf;
  1463.  
  1464.   if (code == TYPE_CODE_ERROR)
  1465.     error ("Function return type unknown.");
  1466.  
  1467.   if (   code == TYPE_CODE_STRUCT
  1468.       || code == TYPE_CODE_UNION)    /* FIXME, implement struct return.  */
  1469.     error ("GDB does not support specifying a struct or union return value.");
  1470.  
  1471.   /* FIXME, this is bogus.  We don't know what the return conventions
  1472.      are, or how values should be promoted.... */
  1473.   if (code == TYPE_CODE_FLT)
  1474.     {
  1475.       dbuf = value_as_double (val);
  1476.  
  1477.       STORE_RETURN_VALUE (VALUE_TYPE (val), (char *)&dbuf);
  1478.     }
  1479.   else
  1480.     {
  1481.       lbuf = value_as_long (val);
  1482.       STORE_RETURN_VALUE (VALUE_TYPE (val), (char *)&lbuf);
  1483.     }
  1484. }
  1485.  
  1486. void
  1487. _initialize_values ()
  1488. {
  1489.   add_cmd ("convenience", no_class, show_convenience,
  1490.         "Debugger convenience (\"$foo\") variables.\n\
  1491. These variables are created when you assign them values;\n\
  1492. thus, \"print $foo=1\" gives \"$foo\" the value 1.  Values may be any type.\n\n\
  1493. A few convenience variables are given values automatically:\n\
  1494. \"$_\"holds the last address examined with \"x\" or \"info lines\",\n\
  1495. \"$__\" holds the contents of the last address examined with \"x\".",
  1496.        &showlist);
  1497.  
  1498.   add_cmd ("values", no_class, show_values,
  1499.        "Elements of value history around item number IDX (or last ten).",
  1500.        &showlist);
  1501. }
  1502. @
  1503.